const express = require('express');
const path = require('path');
const userRouter = require('./route/user');

//引入第三方中间件
const bodyParser = require('body-parser');

const app = express();
app.listen(3000);

app.use(express.static(path.resolve(__dirname, 'public')));
app.use(bodyParser.urlencoded({
    extended: false
}));

app.use('/user', fn);
app.use('/user', userRouter);

function fn(req, res, next) {
    console.log(`中间件fn拦截了${req.url}的请求`);
    console.log(req.query);
    if(req.query.uname !== 'root') {
        res.send("请提供管理员账户");
    } else {
        next();
    }
}

app.post('/login', (req, res) => {
    console.log(req.body);
    res.send('登录成功');
});

